var config = require('../config');
var Authorization_Basic = config.Authorization_Basic;

function request(api, method, header, params, success) {
  wx.showLoading({
    title: '萨达的',
  })
  wx.request({
    url: api,
    method: method,
    header: header,
    data: params,
    success: function(res) {
      // setTimeout(function() {
      //   wx.hideLoading()
      // }, 1000)
      success(res)
    },
    fail: function() {
      wx.showToast({
        icon: "loading",
        title: "请检查网络..."
      })
    }
  })
}
// get请求
var GET_METHOD = "GET"
var GET_HEADER = {
  "Authorization": `${Authorization_Basic}`, //your get header
  "Version": `${config.version}`
}

function get(api, params, success) {
  request(api, GET_METHOD, GET_HEADER, params, success)
}

//post请求
var POST_METHOD = "POST"
var POST_HEADER = {
  "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
  "version": "1.0.0",
  "Authorization": `${Authorization_Basic}` //your post header
}

function post(api, params, success) {
  request(api, POST_METHOD, POST_HEADER, params, success)
}

module.exports = {
  HttpGet: get,
  HttpPost: post
}


/**********
 * 引用封装好的get和post方法
 *var request = require("../../utils/request.js")
 *var api = "yourApi"   //your api
 *var params = {
 *  "id": "yourid",
 *  "name": "yourname"
 *  // ... your params
 *}
 *request.GET(api, params, function (res) {
 *  //do something
 *})
 *request.POST(api, params, function (res) {
 *  //do something
 *})
 ******/